gh-145633: deprecate float.__getformat__() class method#146400
gh-145633: deprecate float.__getformat__() class method#146400skirpichev wants to merge 4 commits intopython:mainfrom
Conversation
| (Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.) | ||
|
|
||
| * The ``__getformat__()`` class method of the :class:`float` is deprecated | ||
| and will be removed in Python 3.20. |
There was a problem hiding this comment.
You should document the deprecation in What's New in Python 3.15.
What is the migration path for projects currently relying on __getformat__()? Do they have to copy/paste the big _have_ieee_doubles() function?
There was a problem hiding this comment.
What is the migration path for projects currently relying on getformat()? Do they have to copy/paste the big _have_ieee_doubles() function?
Primary goal is using the CPython's test suite. No changes needed.
But for the rest - yes.
There was a problem hiding this comment.
Maybe we can simplify helper? I.e. no ctypes.
In float_info docs the C99, section 5.2.4.2.2 was referenced for "details":
The characteristics of floating types are defined in terms of a model that describes a representation of floating-point numbers and values that provide information about an implementation’s floating-point arithmetic. ()
() The floating-point model is intended to clarify the description of each floating-point characteristic and does not require the floating-point arithmetic of the implementation to be identical.
The model is same as in IEEE specification (modulo different summation limits) and we can check that implementation has same set of values for finite numbers, including subnormals and two zeros. And check that there are two infinities and a nan. I.e. slightly extended "if" statement. (The C11 standard has additionally Edit: it's obsoleted in C23.)DBL_HAS_SUBNORM macro. I think we could update float_info with this macro, it will be helpful in such check.
There was a problem hiding this comment.
I still don't understand how to update code using __getformat__()? Should code now assume that Python always support IEEE 754 and so what only matters is the endian? In that case, you should point to sys.byteorder.
There was a problem hiding this comment.
The __getformat__() now is actual only for alternative implementations. They can override this and skip some parts of the CPython test suite (not all in fact related just to specific format of floating-point numbers, but some also expect certain runtime behavior from arithmetic operations).
The _have_ieee_doubles() use some heuristics to check that some unknown alternative Python implementation actually uses IEEE doubles for the float type, at least can encode all stuff. Old __getformat__() just checked one finite number (like we do now with the ctypes codepath in helper). But instead we also could check parameters for floating-point type, presence of infinities/nans/subnormals. I'm not sure which heuristics is better nowadays, but the ctypes module is missing on some platforms.
| # for a discussion of this number. | ||
| SOCK_MAX_SIZE = 16 * 1024 * 1024 + 1 | ||
|
|
||
| def _have_ieee_doubles(): |
There was a problem hiding this comment.
I don't understand well the purpose of this function. Python now requires IEEE 754 as a build dependency: https://docs.python.org/dev/using/configure.html#build-requirements. Is this test to actually check if it's the case?
There was a problem hiding this comment.
This is only for alternative Python implementations, using the CPython test suite.
📚 Documentation preview 📚: https://cpython-previews--146400.org.readthedocs.build/